package com.wswy.wzcx.ui.wzdb

import android.content.Context
import android.graphics.Color
import android.support.v4.content.ContextCompat
import android.text.InputFilter
import android.util.AttributeSet
import android.view.Gravity
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.blankj.utilcode.util.SizeUtils
import com.wswy.wzcx.R
import com.wswy.wzcx.model.wzdb.AddUserCertEntry
import com.wswy.wzcx.module.base.gone
import com.wswy.wzcx.module.base.onClick
import com.wswy.wzcx.module.base.visible
import com.wswy.wzcx.ui.activity.ModuleActivity
import com.wswy.wzcx.ui.dialog.ImgDialog
import com.wswy.wzcx.ui.fragment.JZCFFragment
import com.wswy.wzcx.widget.EventMessage
import org.greenrobot.eventbus.EventBus

class InputAreaLayout : FrameLayout, IInputView {

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attribute: AttributeSet?) : this(context, attribute, 0)

    constructor(context: Context, attribute: AttributeSet?, def: Int) : super(context, attribute, def) {
        setBackgroundColor(Color.WHITE)
        tvLabel = TextView(context)
        tvLabel.textSize = 16.toFloat()
        tvLabel.setTextColor(ContextCompat.getColor(context, R.color.textColorBlack))

        var lp = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        lp.gravity = Gravity.CENTER_VERTICAL
        lp.leftMargin = SizeUtils.dp2px(15.toFloat())
        addView(tvLabel, lp)

        imgHelp = ImageView(context)
        imgHelp.scaleType = ImageView.ScaleType.CENTER
        imgHelp.setImageResource(R.drawable.icon_help)

        imgHelp.gone()
        imgHelp.onClick {
            (it.getTag(R.id.item_data) as? Int)?.runCatching {
                if (this > 0) {
                    ImgDialog(context, this).show()
                }
            }
        }

        lp = LayoutParams(SizeUtils.dp2px(48f), LayoutParams.WRAP_CONTENT)
        lp.gravity = Gravity.CENTER_VERTICAL
        lp.leftMargin = SizeUtils.dp2px(100.toFloat())
        lp.rightMargin = SizeUtils.dp2px(15.toFloat())
        addView(imgHelp, lp)
        etInput = EditText(context)
        etInput.textSize = 16.toFloat()
        etInput.background = null
        etInput.setTextColor(ContextCompat.getColor(context, R.color.textColorBlack))
        etInput.setHintTextColor(ContextCompat.getColor(context, R.color.textHintColor))
        etInput.setSingleLine()

        lp = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        lp.gravity = Gravity.CENTER_VERTICAL
        lp.leftMargin = SizeUtils.dp2px(140.toFloat())
        lp.rightMargin = SizeUtils.dp2px(15.toFloat())

        addView(etInput, lp)

        imgAdd = ImageView(context)
        imgAdd.scaleType = ImageView.ScaleType.CENTER
        imgAdd.setImageResource(R.mipmap.add_js)

        imgAdd.gone()
        imgAdd.onClick {
            (it.getTag(R.id.item_data) as? Int)?.runCatching {
                if (this > 0) {
                    EventBus.getDefault().postSticky(EventMessage("addJz"))
                    context.startActivity(ModuleActivity.getTargetIntent(it.context, JZCFFragment.TAG))

                }
            }
        }
        lp = LayoutParams(SizeUtils.dp2px(48f), LayoutParams.MATCH_PARENT).apply {
            gravity = Gravity.RIGHT
        }
        addView(imgAdd, lp)

    }

    private var tvLabel: TextView
    private var etInput: EditText

    private var imgHelp: ImageView

    private var imgAdd: ImageView

    var certEntry: AddUserCertEntry? = null

    fun setTexts(label: String?, hint: String?) {
        tvLabel.text = label
        etInput.hint = hint ?: "请输入"
    }

    fun getInputText(): String? {
        return etInput.text?.toString()
    }

    override fun bindEntry(index: Int, entry: AddUserCertEntry?) {
        certEntry = entry
        entry?.let {
            val tips = when (it.alias) {
                "DLU", "DLN" -> {
                    //驾照姓名 驾驶证号
                    R.drawable.jiashizheng_zhengye
                }
                "DLFN" -> {
                    //驾驶证档案号
                    R.drawable.jiashizheng_fuben
                }
                "VIC", "ESN", "VLOC" -> {
                    //发动机号 车架号  行驶证车主
                    R.drawable.pic_xingshizheng
                }
                else -> 0
            }

            if (tips > 0) {
                if (index == 0 && (it.alias!!.contains("DLU") ||
                                it.alias!!.contains("DLFN") ||
                                it.alias!!.contains("DLN"))) {
                    imgAdd.setTag(R.id.item_data, tips)
                    imgAdd.visible()
                } else {
                    imgAdd.gone()
                }
                imgHelp.setTag(R.id.item_data, tips)
                imgHelp.visible()
            } else {
                imgHelp.gone()
            }

            val len = it.length ?: 50
            if (len > 0) {
                etInput.filters = arrayOf(InputFilter.LengthFilter(len))
            }
            it.defaultValue?.runCatching {
                if (isNotEmpty()) {
                    etInput.setText(this)
                    etInput.setSelection(length)
                }

            }
        }
        setTexts(entry?.label, entry?.hint)
    }

    override fun getInfo(): AddUserCertEntry? {
        return certEntry
    }

    override fun getValue(): String? {
        return getInputText()
    }

    fun setValue(value: String?) {
        etInput.setText(value)
    }
}